package com.bh.vehicle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.vehicle.domain.JcVehicleHead;
import com.bh.vehicle.mapper.JcVehicleHeadMapper;
import com.bh.vehicle.service.IJcVehicleHeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 车头信息Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcVehicleHeadServiceImpl extends ServiceImpl<JcVehicleHeadMapper, JcVehicleHead> implements IJcVehicleHeadService {
    @Autowired
    private JcVehicleHeadMapper jcVehicleHeadMapper;

    /**
     * 查询车头信息
     *
     * @param id 车头信息主键
     * @return 车头信息
     */
    @DataDictClass
    @Override
    public JcVehicleHead selectJcVehicleHeadById(Long id) {
        return jcVehicleHeadMapper.selectById(id);
    }

    /**
     * 查询车头信息列表
     *
     * @param jcVehicleHead 车头信息
     * @return 车头信息
     */
    @DataDictClass
    @Override
    public List<JcVehicleHead> selectJcVehicleHeadList(JcVehicleHead jcVehicleHead) {
        return jcVehicleHeadMapper.selectJcVehicleHeadList(jcVehicleHead);
    }

    /**
     * 查询车头信息列表
     *
     * @param formOrgId
     * @return 车头信息
     */
    @DataDictClass
    @Override
    public List<JcVehicleHead> selectVehicleExistList(Long jcVehicleId) {
        return jcVehicleHeadMapper.selectVehicleExistList(jcVehicleId);
    }

    /**
     * 新增车头信息
     *
     * @param jcVehicleHead 车头信息
     * @return 结果
     */
    @Override
    public int insertJcVehicleHead(JcVehicleHead jcVehicleHead) {
        if (StringUtils.isBlank(jcVehicleHead.getJcOrganizationId())) {
            jcVehicleHead.setJcOrganizationId(SecurityUtils.getOrgId());
        }
        jcVehicleHead.setCode(jcVehicleHead.getCode().toUpperCase());
        jcVehicleHead.setCreateTime(DateUtils.getNowDate());
        JcVehicleHead result = jcVehicleHeadMapper.vehicleHeadCodeUnique(jcVehicleHead.getCode());
        if (result != null) {
            throw new ServiceException("车头编码已存在");
        }
        return jcVehicleHeadMapper.insert(jcVehicleHead);
    }

    /**
     * 修改车头信息
     *
     * @param jcVehicleHead 车头信息
     * @return 结果
     */
    @Override
    public int updateJcVehicleHead(JcVehicleHead jcVehicleHead) {
        jcVehicleHead.setCode(jcVehicleHead.getCode().toUpperCase());
        JcVehicleHead result = jcVehicleHeadMapper.vehicleHeadCodeUnique(jcVehicleHead.getCode());
        if (result != null && !result.getId().equals(jcVehicleHead.getId())) {
            throw new ServiceException("车头编码已存在");
        }
        return jcVehicleHeadMapper.updateById(jcVehicleHead);
    }

    /**
     * 批量删除车头信息
     *
     * @param ids 需要删除的车头信息主键
     * @return 结果
     */
    @Override
    public int deleteJcVehicleHeadByIds(Long[] ids) {
        return jcVehicleHeadMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除车头信息信息
     *
     * @param id 车头信息主键
     * @return 结果
     */
    @Override
    public int deleteJcVehicleHeadById(Long id) {
        return jcVehicleHeadMapper.deleteById(id);
    }
}
